//
//  ByUISceneOperateView.m
//  laserdevice_ios
//
//  Created by pro on 16/11/25.
//  Copyright © 2016年 com.itboye. All rights reserved.
//

#import "ByUISceneOperateView.h"

#import "ByUIRealTimeVC.h"

@implementation ByUISceneOperateView
{
    UILabel *_distanceLabel;
    
    CGFloat _distance;
}
- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupViews];
    }
    
    return self;
}

- (void)setDistance:(NSString *)distance {
    
    int unit = [ByCommonCache getSystemUnit].intValue;
    CGFloat value = distance.floatValue;
    _distance = value;
    switch (unit) {
        case 0: // 米
            _distanceLabel.text = [NSString stringWithFormat:@"%.3fm", value];
            break;
        case 1: // 英尺
            _distanceLabel.text = [NSString stringWithFormat:@"%.3fft", value * 3.2808399];
            break;
        case 2: // 英寸
            _distanceLabel.text = [NSString stringWithFormat:@"%.2fin", value * 3.2808399 * 12];
            break;
        case 3: // 英尺+英寸
            _distanceLabel.text =  [NSString stringWithFormat:@"%.0f\'%.2f\"", floorf(value * 3.2808399), ((value * 3.2808399) - floorf(value * 3.2808399)) * 12.000];;

            break;
        default:
            break;
    }
    
}

- (void)setupViews {
    
    // 操作框的背景
    CGFloat x, y, width, height;
    x = 0;
    y = 0;
    width = self.width * 0.9;
    height = width / 11;
    UIImageView *operateBgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    operateBgView.userInteractionEnabled = YES;
    operateBgView.contentMode = UIViewContentModeScaleToFill;
    operateBgView.image = [UIImage imageNamed:@"scene_caozuobeijing"];
    [self addSubview:operateBgView];
    operateBgView.x = self.width / 2;
    operateBgView.y = self.height / 2;
    
    NSArray *texts = @[STRING(@"quxiao"),
                       STRING(@"chongzuo"),
                       STRING(@"shanchu"),
                       STRING(@"julixianshi")];
    NSArray *images = @[@"scene_quxiao",
                        @"scene_chongzuo",
                        @"scene_shanchu",
                        @""];
    // 循环创建
    CGFloat margin = 5;
    
    for (int i = 0; i < texts.count; i ++) {
        CGFloat width = operateBgView.width / texts.count;
        CGFloat height = operateBgView.height;
        CGFloat x = i * width;
        CGFloat y = 0;
        UIControl *mixView = [[UIControl alloc] init];
        mixView.frame = CGRectMake(x, y, width, height);
        if (i != texts.count - 1) {
            [mixView addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
        } else {
            UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
            [self addGestureRecognizer:pan];
//            UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
//            [mixView addGestureRecognizer:press];
        }
        [operateBgView addSubview:mixView];
        
        mixView.tag = i;
        
        // 图片
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 14, 14)];
        if ([images[i] length] == 0) {
            imgView.width = 0;
        }
        imgView.image = [UIImage imageNamed:images[i]];
        
        // 文字
        width = [texts[i] getSizeOfString:13].width;
        if (width > mixView.width) {
            width = mixView.width;
        }
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, mixView.height)];
//        label.numberOfLines = 0;
        label.adjustsFontSizeToFitWidth = YES;
        label.font = FONT(13);
        label.text = texts[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        if (i == texts.count - 1) {
            _distanceLabel = label;
        }
        
        [mixView addSubview:imgView];
        [mixView addSubview:label];
        
//        if (i != texts.count - 1) {
//            mixView.width = imgView.width + label.width + 20;
//            if (i == 0) {
//                mixView.left = 0;
//            } else {
//                UIView *lastView = operateBgView.subviews[operateBgView.subviews.count - 2];
//                mixView.left = lastView.right;
//            }
//        } else {
//            UIView *lastView = operateBgView.subviews[operateBgView.subviews.count - 2];
//            mixView.left = lastView.right;
//            mixView.width = operateBgView.width - mixView.left;
//        }
        
        // 设置位置
        CGFloat remainX = mixView.width - imgView.width - label.width - margin;
        imgView.left = remainX / 2;
        imgView.y = mixView.height / 2;
        label.left = imgView.right + margin;
        label.y = mixView.height / 2;
    }
}


- (void)tapAction:(UIControl *)tap {
    
    if (self.didSelectOperation) {
        self.didSelectOperation(tap.tag);
    }
}
- (void)panAction:(UIPanGestureRecognizer *)sender {
    int unit = [ByCommonCache getSystemUnit].intValue;
    CGFloat distance = 0;
    switch (unit) {
        case 0: // 米
            distance = _distance;
            break;
        case 1: // 英尺
            distance = _distance * 3.2808399;
            break;
        case 2: // 英寸
            distance = _distance * 3.2808399 * 12;
            break;
        case 3: // 英尺+英寸
            distance = _distance * 3.2808399;
            break;
        default:
            break;
    }
    
    if (self.moveingDistance) {
        self.moveingDistance(sender, distance);
    }
}
- (void)longPress:(UILongPressGestureRecognizer *)sender {
    
    int unit = [ByCommonCache getSystemUnit].intValue;
    CGFloat distance = 0;
    switch (unit) {
        case 0: // 米
            distance = _distance;
            break;
        case 1: // 英尺
            distance = _distance * 3.2808399;
            break;
        case 2: // 英寸
            distance = _distance * 3.2808399 * 12;
            break;
        case 3: // 英尺+英寸
            distance = _distance * 3.2808399;
            break;
        default:
            break;
    }

    if (self.moveingDistance) {
        self.moveingDistance(sender, distance);
    }
}

@end
